package com.sanji.sjmallmodule.order;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallmodule.goods.GoodsDetailsActivity;
import com.sanji.sjmallutils.ImageUtil;
import com.sanji.sjmallutils.Tools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的订单 item 中的商品adapter
 * Created by zsf on 17-1-15.
 */

public class MyOrderItemAdapter extends BaseAdapter {

    private Context mCtx;

    private List<ShopcarChildBean> mList = new ArrayList<>();

    private int imgWidth;

    public MyOrderItemAdapter(Context context, List<ShopcarChildBean> list) {
        this.mCtx = context;
        this.mList = list;

        imgWidth = ImageUtil.setImageWidth(context, 4);
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public ShopcarChildBean getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(mCtx).inflate(R.layout.adapter_list_item_my_order, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.mAdapterListMyOrderItemImg.setLayoutParams(new RelativeLayout.LayoutParams(imgWidth, imgWidth));

        ShopcarChildBean bean = getItem(i);
        holder.mAdapterListMyOrderItemTitle.setText(bean.getGoodsName());
        Glide.with(mCtx).load(bean.getImgUrl()).centerCrop().placeholder(R.mipmap.default_3j_img)
                //当加载图片失败时，通过error(Drawable drawable)方法设置加载失败后的图片显示
                .error(R.mipmap.default_3j_img)
                .crossFade()
                //加载图片监听
                .into(holder.mAdapterListMyOrderItemImg);
        if (bean.getGoodsType().equals("gift")) {
            holder.mAdapterListMyOrderItemGiftImg.setVisibility(View.VISIBLE);
            holder.mAdapterListMyOrderItemSpec.setText(mCtx.getResources().getString(R.string.color) + bean.getGoodsSpec());
        } else if (bean.getGoodsType().equals("accessories")) {
            holder.mAdapterListMyOrderItemGiftImg.setVisibility(View.GONE);
            holder.mAdapterListMyOrderItemSpec.setText(mCtx.getResources().getString(R.string.color) + bean.getGoodsSpec());
        } else {
            holder.mAdapterListMyOrderItemGiftImg.setVisibility(View.GONE);
            holder.mAdapterListMyOrderItemSpec.setText(mCtx.getResources().getString(R.string.spec) + bean.getGoodsSpec());
        }
        holder.mAdapterListMyOrderItemPrice.setVisibility(View.GONE);
        holder.mAdapterListMyOrderItemGoodsNo.setVisibility(View.VISIBLE);
        holder.mAdapterListMyOrderItemCount.setText("x" + bean.getGoodsCount());
        if (bean.getGoodsType().equals("sku")) {
            holder.mAdapterListMyOrderItemGoodsNo.setText(mCtx.getResources().getString(R.string.goods_no) + bean.getSkuId());
        } else {
            holder.mAdapterListMyOrderItemGoodsNo.setText(mCtx.getResources().getString(R.string.goods_no) + bean.getGoodsId());
        }
        holder.mAdapterListMyOrderItemLayout.setTag(bean);
        holder.mAdapterListMyOrderItemLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShopcarChildBean shopcarChildBean = (ShopcarChildBean) view.getTag();
                String type = shopcarChildBean.getGoodsType();
                if (type.equals("gift")) {
                    Tools.toastTools(mCtx, mCtx.getResources().getString(R.string.gift_no_details));
                } else if (type.equals("sku")) {
                    Intent intent = new Intent(mCtx, GoodsDetailsActivity.class);
                    intent.putExtra("goodsType", "sj");
                    intent.putExtra("goodsCode", shopcarChildBean.getGoodsId());
                    mCtx.startActivity(intent);
                } else if (type.equals("accessories")) {
                    Intent intent = new Intent(mCtx, GoodsDetailsActivity.class);
                    intent.putExtra("goodsType", "accessory");
                    intent.putExtra("goodsCode", shopcarChildBean.getGoodsId());
                    mCtx.startActivity(intent);
                }
            }
        });

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.adapter_list_my_order_item_layout)
        RelativeLayout mAdapterListMyOrderItemLayout;
        @BindView(R.id.adapter_list_my_order_item_img)
        ImageView mAdapterListMyOrderItemImg;
        @BindView(R.id.adapter_list_my_order_item_gift)
        ImageView mAdapterListMyOrderItemGiftImg;
        @BindView(R.id.adapter_list_my_order_item_title)
        TextView mAdapterListMyOrderItemTitle;
        @BindView(R.id.adapter_list_my_order_item_spec)
        TextView mAdapterListMyOrderItemSpec;
        @BindView(R.id.adapter_list_my_order_item_price)
        TextView mAdapterListMyOrderItemPrice;
        @BindView(R.id.adapter_list_my_order_item_goods_no)
        TextView mAdapterListMyOrderItemGoodsNo;
        @BindView(R.id.adapter_list_my_order_item_count)
        TextView mAdapterListMyOrderItemCount;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
